<style lang="less" scoped>
.main-keyword-group {
    .btn-wrap {
        padding: 5px 0;
    }
    .online,
    .offline {
        margin-left: 0;
    }
    .second-row .manage-templet {
        margin-top: 5px;
    }
    .group {
        display: inline-block;
        vertical-align: top;
    }
}
</style>
<template>
    <div class="main main-keyword-group">
        <div class="main-header">
            <el-breadcrumb>
                <el-breadcrumb-item :to="{ path: '/tickets/sell' }">前台验票</el-breadcrumb-item>
                <el-breadcrumb-item>验票记录</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="main-filters">
            <el-form :inline="true" :model="query">
                <el-form-item label="票券编号">
                    <el-input v-model="query.ticket_no"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <div class="main-actions">
            <div class="main-actions-label">
                共<em>{{itemsData.pagination.total}}</em>条
            </div>
            <div class="main-actions-btns">
                <el-button @click="pullItems">批量撤销验票</el-button>
            </div>
        </div>
        <el-table
            border
            element-loading-text="Loading..."
            v-loading.body="!itemsData.$resolved"
            :data="itemsData.items"
            @sort-change="onSortItems"
            @selection-change="onSelect"
        >
            <el-table-column sortable="custom" prop="id" label="ID" width="80"></el-table-column>
            <el-table-column type="selection" width="50"></el-table-column>
            <el-table-column width="250" prop="ticket_no" label="票券编号"></el-table-column>
            <el-table-column width="100" prop="price" label="价格"></el-table-column>
            <!-- <el-table-column width="100" prop="private_price" label="协议价"></el-table-column> -->
            <!-- <el-table-column width="100"  label="票券大类" inline-template>
                <div>
                    <span>{{ row.grandpa ? row.grandpa.name : '-' }}</span>
                </div>
            </el-table-column> -->
            <el-table-column width="200"  label="票券类型" inline-template>
                <div>
                    <span>{{ row.parent ? row.parent.name : '-'}} - {{row.type}}</span>
                </div>
            </el-table-column>
            <!-- <el-table-column label="可用时间" width="200" inline-template>
                <div>
                    <span>{{ row.start_time | date }}-</span>
                    <span>{{ row.end_time | date }}</span>
                </div>
            </el-table-column> -->
            <el-table-column label="验票时间" inline-template>
                <div>
                    <span>{{ row.updated_at | date('YYYY-MM-DD HH:mm:ss')}}</span>
                </div>
            </el-table-column>
            <el-table-column prop="status" label="状态" inline-template>
                <el-tag :type="statusAlias[row.status].icon">{{statusAlias[row.status].name}}</el-tag>
            </el-table-column>
            <!-- <el-table-column label="状态属性" width="100" inline-template>
                <div>
                    <el-tag type="success" v-if="row.status === 1">已上架</el-tag>
                    <el-tag type="warning" v-else>已下架</el-tag>
                </div>
            </el-table-column> -->
            <!-- <el-table-column sortable="custom" prop="priority" label="排序"></el-table-column> -->
            <el-table-column label="操作" inline-template>
                <div class="item-menus">
                    <el-button size="small" type="danger" @click="pullItem(row)">撤销验票</el-button>
                    <!-- <el-button size="small" type="success" v-if="row.grand_id === 0" @click="editItem(row, 2)">新增子票券</el-button> -->
                </div>
            </el-table-column>
        </el-table>

        <el-pagination
            v-if="itemsData.$resolved"
            layout="prev, pager, next, jumper"
            :total="itemsData.pagination.total"
            :current-page="query.page_num"
            :page-size="query.page_size"
            @current-change="onPageChange"
        >
        </el-pagination>
    </div>
</template>

<script>
import ListController from '../controllers/list';

import TicketModel from '../models/ticket';
import TypeModel from '../models/type';
import CustomModel from '../models/customer';
import Promise from 'bluebird';

export default new ListController({
    data() {
        return {
            ListModel: TicketModel,

            query: {
                updated_at: new Date(Date.now() - (60 * 60 * 1000)),
                status: 4,
                ticket_no: '',
                customer_id: 'all'
            },

            statusMap: {
                PENDING: TicketModel.PENDING,
                USED: TicketModel.USED,
                OFFLINE: TicketModel.OFFLINE,
                UNUSED: TicketModel.UNUSED,
                COMPLETED: TicketModel.COMPLETED
            },
            statusAlias: {
                [TicketModel.USED]: {
                    icon: 'success',
                    name: '未核销'
                },
                [TicketModel.COMPLETED]: {
                    icon: 'success',
                    name: '已核销'
                }
            },

            parents: [],
            grands: [],
            customers: [{
                id: 1,
                name: '中国银行'
            }, {
                id: 1,
                name: '农业银行'
            }],

            editData: {},
            editDialogVisible: false
        };
    },
    methods: {
        beforeLoadItems() {
            return Promise.all([
                this.loadParents(),
                this.loadGrands(),
                this.loadCustomers()
            ]);
        },
        loadGrands() {
            return TypeModel.query({
                parent_id: 0,
                grand_id: 0
            })
            .$promise
            .then(types => {
                this.grands = types.items;
            });
        },
        loadParents() {
            return TypeModel.query({
                grand_id: 0
            })
            .$promise
            .then(types => {
                this.parents = types.items;
            });
        },
        loadCustomers() {
            return CustomModel.query({
            })
            .$promise
            .then(types => {
                this.customers = types.items;
            });
        },
        debugItem(row) {
            this.$confirm('确认调试该项？')
            .then(() => {
                this.ListModel.update({
                    id: row.id,
                    status: 1
                }).$promise;
            })
            .catch(err => {
                throw new Error(err);
            });
        },
        pullItem(row) {
            this.$confirm('确认撤销验票？')
            .then(() => {
                const params = Object.assign({}, row, {
                    id: row.id,
                    status: TicketModel.UNUSED,
                    type: 'cancel'
                });
                this.ListModel.update(params).$promise;
            })
            .then(() => {
                this.$message({
                    message: '撤销验票成功！',
                    type: 'success'
                });

                this.loadItems();
            })
            .catch(err => {
                this.$message.error(err);
            });
        },
        pullItems() {
            return this.setItems(this.selectedItems, {
                status: TicketModel.UNUSED,
                type: 'cancel'
            }, {
                confirm: '确认撤销验票？'
            })
            .then(() => {
                 this.$message({
                    message: '撤销验票成功！',
                    type: 'success'
                });

                this.loadItems()
            })
        },
        pushItem(row) {
            this.$confirm('确认上线该项？')
            .then(() => {
                return this.ListModel.update({
                    id: row.id,
                    status: 1
                }).$promise
                .then(() => {
                    this.loadItems();
                });
            })
            .catch(err => {
                throw new Error(err);
            });
        },
        deleteItem(row) {
            this.$confirm('确认删除该票券？')
            .then(() => {
                return this.ListModel.delete({
                    id: row.id
                }).$promise;
            })
            .then(() => {
                return this.loadItems();
            })
            .catch(err => {
                throw new Error(err);
            });
        },
        editItem(row, type) {
            this.editDialogVisible = true;
            if(type === 1 || !type) {
                this.editData = Object.assign({}, {name: ''}, row);
                return;
            }
            if(row && row.parent_id === 0) {
                this.editData = Object.assign({}, {name: '', parent_id: row.id });
                return;
            }

            if(row && row.grand_id === 0) {
                this.editData = Object.assign({}, {name: '', parent_id: row.id, grand_id: row.parent_id });
            }           
        },
        submitEdit() {
            let params = null;
            let $submitPromise = null;

            if(this.editData.id > 0) {
                $submitPromise = this.ListModel.update(this.editData).$promise;
            }
            else {
                params = Object.assign({}, this.editData);
                $submitPromise = this.ListModel.save(params).$promise;
            }

            $submitPromise
            .then(res => {
                if(res.code && res.code !== 200) {
                    this.$message.error(res.message);
                    return;
                }
                this.editDialogVisible = false;
                this.$message({
                    message: '提交成功！',
                    type: 'success'
                });
                this.loadItems();
            })
            .catch(err => {
                this.$message.error(err);
            });
        }
    }
});
</script>
